using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using FileFolderNavigator;
using Microsoft.Win32;

namespace FolderBackupInterface
{
    public partial class Form1 : Form
    {
        //private Location src;
        //private Location dst;
        //private Location xml;
        //private Action act;
        private XmlDocument doc;

        private List<ProjectElement> projects;
        public Form1()
        {
            InitializeComponent();
            //textBox1.TextChanged += new EventHandler(textBox1_TextChanged);
            ProjectName.TextChanged += new EventHandler(ProjectName_TextChanged);
            listBox1.Click += new EventHandler(listBox1_Click);
            listBox1.SelectedIndexChanged += new EventHandler(listBox1_SelectedIndexChanged);
            //XmlDocument doc = new XmlDocument();
            //doc.Load("XMLFile1.xml");
            //projects = BuildProectList(doc);

            //UpdateListBox();

            src.DisableAll = true;
            dst.DisableAll = true;
            //xml = new Location();
            //act = new Action();


            //src.Dock = DockStyle.Fill;
            //dst.Dock = DockStyle.Fill;
            //xml.Dock = DockStyle.Fill;
            //act.Dock = DockStyle.Fill;

            textBox1.Text = Registry.LocalMachine.OpenSubKey("SOFTWARE").OpenSubKey("BNProd").OpenSubKey("FileBackUp").GetValue("projectsxml").ToString();
        }

        void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            ProjectElement selected = (ProjectElement)((ListBox)sender).SelectedItem;
            SetValues(selected);
        }

        void ProjectName_TextChanged(object sender, EventArgs e)
        {
            ProjectElement elem = (ProjectElement)listBox1.SelectedItem;

            if (elem != null)
            {
                elem.Name = ((TextBox)sender).Text;
                int selind = listBox1.SelectedIndex;

                UpdateListBox();
                listBox1.SelectedIndex = selind;
            }
        }

        void listBox1_Click(object sender, EventArgs e)
        {
            //ProjectElement selected = (ProjectElement)((ListBox)sender).SelectedItem;
            //SetValues(selected);
        }

        private void SetValues(ProjectElement selected)
        {
            src.SetValues(selected.Source);
            dst.SetValues(selected.Destination);
            xml.SetValues(selected.XmlDoc);
            act.SetValues(selected.Action);
            ProjectName.Text = selected.Name;
        }

        private void UpdateListBox() 
        {
            ListBox l = listBox1;
            l.Items.Clear();

            foreach (ProjectElement proj in projects)
            {
                l.Items.Add((object)proj);
            }
        }

        private List<ProjectElement> BuildProjectList(XmlDocument doc)
        {
            List<ProjectElement> p = new List<ProjectElement>();
            XmlNodeList list = doc.SelectNodes("//project");
            foreach (XmlElement elem in list)
            {
                p.Add(new ProjectElement(elem));
            }
            return p;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            doc.Save(textBox1.Text);
        }

        private void BrowseButton_Click(object sender, EventArgs e)
        {
            openFileDialog1.DefaultExt = "xml";
            DialogResult result = openFileDialog1.ShowDialog();
            if (result == DialogResult.OK)
            {
                textBox1.Text = openFileDialog1.FileName;
            }
        }

        private void LoadButton_Click(object sender, EventArgs e)
        {
            doc = new XmlDocument();
            doc.Load(textBox1.Text);
            projects = BuildProjectList(doc);

            UpdateListBox();
            listBox1.SelectedIndex = 0;

            ProjectElement selected = (ProjectElement)listBox1.SelectedItem;
            SetValues(selected);
        }

        private void NewButton_Click(object sender, EventArgs e)
        {
            projects.Add(ProjectElement.CreateNewProject(doc));
            UpdateListBox();
            listBox1.SelectedIndex = listBox1.Items.Count - 1;
        }

        private void DeleteButton_Click(object sender, EventArgs e)
        {
            ProjectElement elem = (ProjectElement)listBox1.SelectedItem;
            elem.Delete();
            projects = BuildProjectList(doc);
            UpdateListBox();
            listBox1.SelectedIndex = 0;
        }

        private void ExecuteButton_Click(object sender, EventArgs e)
        {
            ProjectElement selected = (ProjectElement)listBox1.SelectedItem;
            //FolderBackup.ExecuteProject(selected);
            FolderBackup.ExecuteProjectCheckTime(selected);
            button1_Click(sender, e);
            LoadButton_Click(sender, e);
        }
    }
}